/**
 * //字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 给定两个字符串，编写一个函数判定它们是否只需要一次(或者零次)编辑。
 * //
 * //
 * //
 * // 示例 1:
 * //
 * // 输入:
 * //first = "pale"
 * //second = "ple"
 * //输出: True
 * //
 * //
 * //
 * // 示例 2:
 * //
 * // 输入:
 * //first = "pales"
 * //second = "pal"
 * //输出: False
 * //
 * // Related Topics 双指针 字符串 👍 207 👎 0
 */

package com.xixi.medium;

public class ID_Interview_01_05_OneAwayLcci {
public static void main(String[]args){
        Solution solution=new ID_Interview_01_05_OneAwayLcci().new Solution();
        }


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public boolean oneEditAway(String first, String second) {

        int difference = first.length() - second.length();
        if (Math.abs(difference) > 1) {
            return false;
        }

        char[] firstChar = first.toCharArray();
        char[] secondChar = second.toCharArray();

        //找到不同的点
        int firstIndex = 0;
        int secondIndex = 0;
        while (firstIndex < first.length() && secondIndex < second.length() && firstChar[firstIndex] == secondChar[secondIndex]) {
            firstIndex++;
            secondIndex++;
        }


        if (difference == 1) { //减少了一个字符
            if(second.length() == secondIndex){
                return true;
            }
            firstIndex = firstIndex + 1;


        } else if (difference == -1) { //增加一个字符
            secondIndex = secondIndex + 1;
        } else if (difference == 0) { //替换一个字符
            if (firstIndex == first.length()) { //没有改动
                return true;
            } else {
                firstIndex++;
                secondIndex++;
            }

        }

        while(firstIndex < first.length() && secondIndex < second.length()){
            if(firstChar[firstIndex] != secondChar[secondIndex]){
                return false;
            }
            firstIndex++;
            secondIndex++;
        }

        return true;


    }
}
//leetcode submit region end(Prohibit modification and deletion)




}